package com.shengdong.water.code.widget

import android.annotation.SuppressLint
import android.content.Context
import android.content.res.Resources
import android.content.res.TypedArray
import android.graphics.Color
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.EditText
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.appcompat.widget.AppCompatEditText
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.content.ContextCompat
import com.shengdong.water.R
import androidx.core.content.withStyledAttributes

/**
 * 多功能设置项控件
 * 支持六种模式：
 * 1. 普通模式：左侧标题 + 右侧箭头
 * 2. 文字模式：左侧标题 +右侧文字
 * 3. 文字+箭头模式：左侧标题 + 右侧文字 + 右箭头
 * 4. 下拉选择模式：左侧标题 + 右侧内容 + 下拉箭头
 * 5. 输入模式：左侧标题 + 右侧输入框
 * 6. 带单位输入模式：左侧标题 + 右侧输入框 + 单位

 */
class LabelItemView @JvmOverloads constructor(

    context: Context,

    attrs: AttributeSet? = null,

    defStyleAttr: Int = 0

) : ConstraintLayout(context, attrs, defStyleAttr) {

    companion object {

        const val MODE_NORMAL = 0       // 普通模式 + 箭头
        const val MODE_TEXT = 1         // 文字模式 +无箭头
        const val MODE_TEXT_ARROW = 2   // 文字+箭头模式
        const val MODE_DROPDOWN = 3    // 下拉选择模式
        const val MODE_INPUT = 4        // 输入模式
        const val MODE_INPUT_UNIT = 5   // 带单位输入模式
    }

    // 控件引用
    private val titleTextView: TextView

    private val contentTextView: TextView

    private val inputEditText: AppCompatEditText

    private val unitTextView: TextView

    private val arrowImageView: ImageView

    private val dividerView: View

    // 属性
    private var mode: Int = MODE_NORMAL

    private var showDivider: Boolean = true

    // 回调
    private var onClickListener: OnClickListener? = null

    private var onContentChangeListener: ((String) -> Unit)? = null

    init {
        // 加载布局
        LayoutInflater.from(context).inflate(R.layout.layout_label_item, this, true)
        // 获取控件引用
        titleTextView = findViewById(R.id.tv_title)
        contentTextView = findViewById(R.id.tv_content)
        inputEditText = findViewById(R.id.et_input)
        unitTextView = findViewById(R.id.tv_unit)
        arrowImageView = findViewById(R.id.iv_arrow)
        dividerView = findViewById(R.id.view_divider)
        // 设置默认高度
        val defaultHeight = resources.getDimensionPixelSize(R.dimen.dp50)

        layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, defaultHeight)

        // 读取自定义属性
        context.withStyledAttributes(attrs, R.styleable.LabelItemView) {
            initAttributes(this)

        }


        // 设置点击事件

        super.setOnClickListener {

            onClickListener?.onClick(it)

        }

    }


    private fun initAttributes(typedArray: TypedArray) {

        // 读取属性

        val title = typedArray.getString(R.styleable.LabelItemView_liv_title) ?: ""

        val content = typedArray.getString(R.styleable.LabelItemView_liv_content) ?: ""

        val unit = typedArray.getString(R.styleable.LabelItemView_liv_unit) ?: ""

        val contentHint = typedArray.getString(R.styleable.LabelItemView_liv_content_hint) ?: ""

        mode = typedArray.getInt(R.styleable.LabelItemView_liv_mode, MODE_NORMAL)

        showDivider = typedArray.getBoolean(R.styleable.LabelItemView_liv_show_divider, true)


        // 设置标题

        titleTextView.text = title

        // 设置颜色和大小

        val titleColor = typedArray.getColor(
            R.styleable.LabelItemView_liv_title_color,
            titleTextView.currentTextColor
        )

        val contentColor = typedArray.getColor(
            R.styleable.LabelItemView_liv_content_color,
            contentTextView.currentTextColor
        )

        val hintColor = typedArray.getColor(
            R.styleable.LabelItemView_liv_hint_color,
            unitTextView.currentTextColor
        )

        val dividerColor = typedArray.getColor(
            R.styleable.LabelItemView_liv_divider_color,
            dividerView.background.constantState?.newDrawable()
                ?.let { it as? android.graphics.drawable.ColorDrawable }?.color ?: 0xE0E0E0
        )


        val titleSize = typedArray.getDimensionPixelSize(
            R.styleable.LabelItemView_liv_title_size,
            titleTextView.textSize.toInt()
        )

        val contentSize = typedArray.getDimensionPixelSize(
            R.styleable.LabelItemView_liv_content_size,
            contentTextView.textSize.toInt()
        )

        val hintSize = typedArray.getDimensionPixelSize(
            R.styleable.LabelItemView_liv_hint_size,
            unitTextView.textSize.toInt()
        )

        val dividerHeight = typedArray.getDimensionPixelSize(
            R.styleable.LabelItemView_liv_divider_height,
            dividerView.layoutParams.height
        )

        val dividerMargin =
            typedArray.getDimensionPixelSize(R.styleable.LabelItemView_liv_divider_margin, 0)



        titleTextView.setTextColor(titleColor)

        contentTextView.setTextColor(contentColor)

        inputEditText.setTextColor(contentColor)

        unitTextView.setTextColor(hintColor)

        dividerView.setBackgroundColor(dividerColor)



        titleTextView.textSize = titleSize / resources.displayMetrics.density

        contentTextView.textSize = contentSize / resources.displayMetrics.density

        inputEditText.textSize = contentSize / resources.displayMetrics.density

        unitTextView.textSize = hintSize / resources.displayMetrics.density


        // 设置分割线

        dividerView.layoutParams.height = dividerHeight

        (dividerView.layoutParams as MarginLayoutParams).setMargins(
            dividerMargin,
            0,
            dividerMargin,
            0
        )

        dividerView.visibility = if (showDivider) View.VISIBLE else View.GONE


        // 根据模式设置控件状态

        when (mode) {

            MODE_NORMAL -> {

                contentTextView.visibility = View.GONE

                inputEditText.visibility = View.GONE

                unitTextView.visibility = View.GONE

                arrowImageView.visibility = View.VISIBLE

                arrowImageView.setImageResource(R.mipmap.ic_arrow_right)

            }

            MODE_TEXT -> {

                contentTextView.visibility = View.VISIBLE

                contentTextView.text = content

                inputEditText.visibility = View.GONE

                unitTextView.visibility = View.GONE

                arrowImageView.visibility = View.GONE

            }

            MODE_TEXT_ARROW -> {

                contentTextView.visibility = View.VISIBLE

                contentTextView.text = content

                inputEditText.visibility = View.GONE

                unitTextView.visibility = View.GONE

                arrowImageView.visibility = View.VISIBLE

                arrowImageView.setImageResource(R.mipmap.ic_arrow_right)


            }

            MODE_DROPDOWN -> {

                contentTextView.visibility = View.VISIBLE

                contentTextView.text = content

                inputEditText.visibility = View.GONE

                unitTextView.visibility = View.GONE

                arrowImageView.visibility = View.VISIBLE

                arrowImageView.setImageResource(R.mipmap.ic_arrow_right)

            }

            MODE_INPUT -> {

                contentTextView.visibility = View.GONE

                inputEditText.visibility = View.VISIBLE

                inputEditText.setText(content)

                inputEditText.hint = contentHint

                unitTextView.visibility = View.GONE

                arrowImageView.visibility = View.GONE

                setupInputEditText()

            }

            MODE_INPUT_UNIT -> {

                contentTextView.visibility = View.GONE

                inputEditText.visibility = View.VISIBLE

                inputEditText.setText(content)

                inputEditText.hint = contentHint

                unitTextView.visibility = View.VISIBLE

                unitTextView.text = unit

                arrowImageView.visibility = View.GONE

                setupInputEditText()

            }


        }

    }


    private fun setupInputEditText() {
        // 设置输入框监听

        inputEditText.setOnFocusChangeListener { _, hasFocus ->

            if (!hasFocus) {

                onContentChangeListener?.invoke(inputEditText.text.toString())

            }

        }

    }


    // 设置输入类型

    fun setInputType(inputType: Int) {

        inputEditText.inputType = inputType

    }


    // 设置标题

    fun setTitle(title: String) {

        titleTextView.text = title

    }


    // 设置内容

    fun setContent(content: String) {

        when (mode) {

            MODE_TEXT, MODE_DROPDOWN, MODE_TEXT_ARROW -> contentTextView.text = content

            MODE_INPUT, MODE_INPUT_UNIT -> inputEditText.setText(content)

        }

    }

    fun setContentHint(hint: String) {

        when (mode) {
            MODE_INPUT, MODE_INPUT_UNIT -> inputEditText.hint = hint

        }

    }


    fun setContentColor() {

        when (mode) {
            MODE_TEXT, MODE_DROPDOWN, MODE_TEXT_ARROW -> contentTextView.setTextColor(
                ContextCompat.getColor(
                    context,
                    R.color.textColor
                )
            )

            MODE_INPUT, MODE_INPUT_UNIT -> inputEditText.setTextColor(
                ContextCompat.getColor(
                    context,
                    R.color.textColor
                )
            )

        }

    }


    // 获取内容

    fun getContent(): String {

        return when (mode) {

            MODE_TEXT, MODE_DROPDOWN, MODE_TEXT_ARROW -> contentTextView.text.toString()

            MODE_INPUT, MODE_INPUT_UNIT -> inputEditText.text.toString()

            else -> ""

        }

    }


    // 设置单位

    fun setUnit(unit: String) {

        if (mode == MODE_INPUT_UNIT) {

            unitTextView.text = unit

        }

    }


    // 设置是否显示分割线

    fun setShowDivider(show: Boolean) {

        showDivider = show

        dividerView.visibility = if (show) View.VISIBLE else View.GONE

    }


    // 设置模式

    fun setMode(newMode: Int) {

        if (mode == newMode) return



        mode = newMode


        // 根据模式设置控件状态

        when (mode) {

            MODE_NORMAL -> {

                contentTextView.visibility = View.GONE

                inputEditText.visibility = View.GONE

                unitTextView.visibility = View.GONE

                arrowImageView.visibility = View.VISIBLE

                arrowImageView.setImageResource(R.mipmap.ic_arrow_right)

            }

            MODE_TEXT -> {
                contentTextView.visibility = View.VISIBLE

                inputEditText.visibility = View.GONE

                unitTextView.visibility = View.GONE

                arrowImageView.visibility = View.GONE
            }


            MODE_DROPDOWN -> {

                contentTextView.visibility = View.VISIBLE

                inputEditText.visibility = View.GONE

                unitTextView.visibility = View.GONE

                arrowImageView.visibility = View.VISIBLE

                arrowImageView.setImageResource(R.mipmap.ic_arrow_right)

            }

            MODE_INPUT -> {

                contentTextView.visibility = View.GONE

                inputEditText.visibility = View.VISIBLE

                unitTextView.visibility = View.GONE

                arrowImageView.visibility = View.GONE

                setupInputEditText()

            }

            MODE_INPUT_UNIT -> {

                contentTextView.visibility = View.GONE

                inputEditText.visibility = View.VISIBLE

                unitTextView.visibility = View.VISIBLE

                arrowImageView.visibility = View.GONE

                setupInputEditText()

            }

            MODE_TEXT_ARROW -> {

                contentTextView.visibility = View.VISIBLE

                inputEditText.visibility = View.GONE

                unitTextView.visibility = View.GONE

                arrowImageView.visibility = View.VISIBLE

                arrowImageView.setImageResource(R.mipmap.ic_arrow_right)

            }

        }

    }


    // 设置内容变化监听

    fun setOnContentChangeListener(listener: (String) -> Unit) {

        onContentChangeListener = listener

    }


    // 重写点击事件，以便能够添加自定义逻辑

    override fun setOnClickListener(listener: OnClickListener?) {

        onClickListener = listener

    }

    /**
     * 设置箭头是否显示
     */
    fun setShowArrow(show: Boolean) {
        arrowImageView.visibility = if (show) View.VISIBLE else View.GONE
    }
}